package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type CaseManagementService struct{}

// CreateCaseManagement 创建案件管理记录
// Author [yourname](https://github.com/yourname)
func (CMService *CaseManagementService) CreateCaseManagement(ctx context.Context, CM *flaud.CaseManagement) (err error) {
	err = global.GVA_DB.Create(CM).Error
	return err
}

// DeleteCaseManagement 删除案件管理记录
// Author [yourname](https://github.com/yourname)
func (CMService *CaseManagementService) DeleteCaseManagement(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.CaseManagement{}, "id = ?", ID).Error
	return err
}

// DeleteCaseManagementByIds 批量删除案件管理记录
// Author [yourname](https://github.com/yourname)
func (CMService *CaseManagementService) DeleteCaseManagementByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.CaseManagement{}, "id in ?", IDs).Error
	return err
}

// UpdateCaseManagement 更新案件管理记录
// Author [yourname](https://github.com/yourname)
func (CMService *CaseManagementService) UpdateCaseManagement(ctx context.Context, CM flaud.CaseManagement) (err error) {
	err = global.GVA_DB.Model(&flaud.CaseManagement{}).Where("id = ?", CM.ID).Updates(&CM).Error
	return err
}

// GetCaseManagement 根据ID获取案件管理记录
// Author [yourname](https://github.com/yourname)
func (CMService *CaseManagementService) GetCaseManagement(ctx context.Context, ID string) (CM flaud.CaseManagement, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&CM).Error
	return
}

// GetCaseManagementInfoList 分页获取案件管理记录
// Author [yourname](https://github.com/yourname)
func (CMService *CaseManagementService) GetCaseManagementInfoList(ctx context.Context, info flaudReq.CaseManagementSearch) (list []flaud.CaseManagement, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.CaseManagement{})
	var CMs []flaud.CaseManagement
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["case_number"] = true
	orderMap["year"] = true
	orderMap["cause"] = true
	orderMap["legal_basis"] = true
	orderMap["amount_involved"] = true
	orderMap["evidence_type"] = true
	orderMap["perpetrator_trait"] = true
	orderMap["crime_elements"] = true
	orderMap["mitigating_circumstances"] = true
	orderMap["dependent_variables"] = true
	orderMap["sentence_after_conviction"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&CMs).Error
	return CMs, total, err
}
func (CMService *CaseManagementService) GetCaseManagementPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
